/****************************************************************************
 *
 *  LinuxChatty
 *  Copyright (C) 2010, David Hsu.
 * 
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License along
 *  with this program; if not, write to the Free Software Foundation, Inc.,
 *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 ***************************************************************************/
#include "RadioGroup.h"
#include "RadioButton.h"
#include <set>

using namespace std;

RadioGroup::RadioGroup():
	currentSelected(NULL)
{
	// None for now, just in case
}

/**
 * Adds a radio button to this control group.
 * @param rb RadioButton pointer to add.
 */
void RadioGroup::AddRadioButton(RadioButton * rb)
{
	radioSet.insert(rb);
	UpdateRadios(rb);
}

/**
 * Checks to see if the radio button that changed.
 * @param rb Recent RadioButton that changed
 */
void RadioGroup::UpdateRadios(RadioButton * rb)
{
	if (rb->IsSelected() && rb != currentSelected)
	{
		// Make sure this radio button is part of this group
		set<RadioButton *>::iterator it;
		it = radioSet.find(rb);
		if (it != radioSet.end())
		{
			if (currentSelected != NULL)
				currentSelected->SetSelection(false);
			rb->SetSelection(true);
			currentSelected = rb;
		}
	}
}
